package edu.ccut.saturn.studio.naviagtor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import edu.ccut.saturn.studio.core.exception.XMLException;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;

public class SaturnDirectory {

	public static final String TYPE_ACTION = "action";
	public static final String TYPE_LOGIC = "logic";
	public static final String TYPE_COMPONENT = "component";
	public static final String TYPE_DATA = "data";
	public static final String TYPE_SATURN_DATA = "saturndata";
	public static final String TYPE_VALIDATE = "validate";
	public static final String TYPE_PAGE = "web";
	public static final String TYPE_JSP_PAGE = "jsp";
	public static final String TYPE_WORKFLOW = "workflow";
	public static final String TYPE_MODEL = "model";

	private String name = "";
	private String version = "";
	private String description = "";
	private String type = "";
	private String time = "";

	public static SaturnDirectory loadSaturnDirectory(InputStream in) {
		SaturnDirectory saturnDirectory = new SaturnDirectory();

		try {
			Document document = SaturnW3CUtil.getDocument(in);
			Element root = document.getDocumentElement();

			saturnDirectory
					.setName(SaturnW3CUtil.getChildNodeValue(root, "name"));
			saturnDirectory.setVersion(SaturnW3CUtil.getChildNodeValue(root,
					"version"));
			saturnDirectory.setDescription(SaturnW3CUtil.getChildNodeValue(root,
					"description"));
			saturnDirectory
					.setType(SaturnW3CUtil.getChildNodeValue(root, "type"));
			saturnDirectory
					.setTime(SaturnW3CUtil.getChildNodeValue(root, "time"));
		} catch (XMLException e) {
			e.printStackTrace();
		}

		return saturnDirectory;
	}

	public static SaturnDirectory loadSaturnDirectory(IFile file) {
		InputStream inputStream = null;
		SaturnDirectory saturnDirectory = null;

		if (file.exists()) {
			try {
				inputStream = file.getContents();
				saturnDirectory = SaturnDirectory.loadSaturnDirectory(inputStream);

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}

		return saturnDirectory;
	}
	
	public static SaturnDirectory loadSaturnDirectory(File file) {
		InputStream inputStream = null;
		SaturnDirectory saturnDirectory = null;

		if (file.exists()) {
			try {
				inputStream = new FileInputStream(file);
				saturnDirectory = SaturnDirectory.loadSaturnDirectory(inputStream);

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}

		return saturnDirectory;
	}

	public static void writeSaturnDirectory(InputStream in,
			SaturnDirectory saturnDirectory) {
		try {
			Document document = SaturnW3CUtil.getDocument(in);
			Element root = document.getDocumentElement();

			SaturnW3CUtil.setChildNodeValue(root, "name", saturnDirectory
					.getName());
			SaturnW3CUtil.setChildNodeValue(root, "version", saturnDirectory
					.getVersion());
			SaturnW3CUtil.setChildNodeValue(root, "description", saturnDirectory
					.getDescription());
			SaturnW3CUtil.setChildNodeValue(root, "type", saturnDirectory
					.getType());
			SaturnW3CUtil.setChildNodeValue(root, "time", saturnDirectory
					.getTime());
		} catch (XMLException e) {
			e.printStackTrace();
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}
}
